package cc.vv.btpartyjob.homepage.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cc.vv.baselibrary.adapter.LKBaseAdapter;
import cc.vv.baselibrary.api.BaseUrl;
import cc.vv.baselibrary.view.LKCircleImageView;
import cc.vv.btpartyjob.homepage.R;
import cc.vv.btpartyjob.homepage.moudel.bean.rank.BranchRankingObj;
import cc.vv.lkimagecomponent2.LKImage;

/**
 * 学习总排名
 * Created by CHP on 2017/3/31.
 */

public class LearnRankAllAdapter extends LKBaseAdapter<BranchRankingObj> {

    public LearnRankAllAdapter(ArrayList<BranchRankingObj> objList, Activity activity) {
        super(objList, activity);
    }

    @Override
    protected View lpgetView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mActivity, R.layout.adapter_learn_all_rank, null);
            holder = new ViewHolder();
            holder.iv_all_head = convertView.findViewById(R.id.iv_all_head);
            holder.iv_ranking_no = convertView.findViewById(R.id.iv_ranking_no);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_time = convertView.findViewById(R.id.tv_time);
            holder.tv_count = convertView.findViewById(R.id.tv_count);
            holder.iv_ranking_emblem = convertView.findViewById(R.id.iv_ranking_emblem);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final BranchRankingObj data = mObjList.get(position);


        holder.iv_ranking_no.setText(position + 1 + "");
        if (data != null) {

            //头像
            LKImage.load().placeHolder(R.mipmap.icon_def_image)
                    .error(R.mipmap.icon_def_image).load(BaseUrl.getYanAnUrl() + data.getPhotoPath()).into(holder.iv_all_head);
            holder.tv_name.setText(data.getName());
//            if (user.getIsDoed() == 1) {
//                holder.iv_ranking_emblem.setImageResource(R.mipmap.icon_emblem_red);
//            } else {
//                holder.iv_ranking_emblem.setImageResource(R.mipmap.icon_emblem_gray);
//            }
            if (data.getTotalPraiseCount() == 0) {
                holder.tv_count.setText(data.getMonthPraiseCount() + "");
            } else {
                holder.tv_count.setText(data.getTotalPraiseCount() + "");
            }
        }
        holder.tv_time.setText(time(data.getTotalHours() + ""));

        return convertView;
    }


    private final class ViewHolder {
        LKCircleImageView iv_all_head;
        TextView iv_ranking_no;//次序
        TextView tv_name;//名字
        TextView tv_time;//时间
        TextView tv_count;//点赞数量
        ImageView iv_ranking_emblem;//点赞
    }

    /**
     * 时间转换
     *
     * @param time
     * @return
     */
    private String time(String time) {
        Long total = Long.parseLong(time);

        if (total / 3600 == 0 && total / 60 == 0) {
            return 0 + "分钟";
        } else if (total / 3600 == 0 && total / 60 > 0) {
            return total / 60 + "分钟";
        } else if ((total / 3600) > 0 && (total / (3600 * 24)) == 0) {
            return total / 3600 + "小时" + ((total - ((total / 3600) * 3600)) / 60) + "分钟";

        } else if ((total / (3600 * 24)) > 0 && (total / (3600 * 24 * 30)) == 0) {
            return (total / (3600 * 24)) + "天" + ((total - ((total / (3600 * 24)) * (3600 * 24))) / 3600) + "小时";
        } else if ((total / (3600 * 24 * 30)) > 0) {
            return (total / (3600 * 24 * 30)) + "月" + ((total - ((total / (3600 * 24 * 30)) * (3600 * 24 * 30))) / (3600 * 24)) + "天";
        }
        return 0 + "分钟";
    }
}
